<?php
!defined('P_W') && exit('Forbidden');
require_once MODE . '/base/basehelper.php';

/**
 * 房源管理helper
 * @author bingbing.renbb
 *
 */
class PW_ManageHelper extends BaseHelper{
	
	/**
	 * 删除一个或多个房源
	 * @param mixed $houses 房子id
	 * @param string $type  房子类型，sale或hire
	 */
	function delete($houses, $type) {
		$houses = explode(',',$houses);
		$allow = $this->_checkAllows($houses, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		if ($houseService->delete($houses)) {
			echo getLangInfo('msg','operate_success')."\treload";
			ajax_footer();
		}
	}
	
	/**
	 * 下架一个或多个房源
	 * @param mixed $houses 房子id
	 * @param string $type  房子类型，sale或hire
	 */
	function except($houses, $type) {
		$houses = explode(',',$houses);
		$allow = $this->_checkAllows($houses, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		if($houseService->setState($houses,1)){	
			if ($type == 'hire') {
				echo "房源下架成功\thire\t".BASEURL.'q=manage&a=hire&type=prepare';
			} else {
				echo "房源下架成功\tsale\t".BASEURL.'q=manage&a=sale&type=prepare';
			}
			ajax_footer();
		}
	}
	
	/**
	 * 上架一个或多个房源
	 * @param mixed $houses 房子id
	 * @param string $type  房子类型，sale或hire
	 */
	function uphouse($houses, $type) {
		$houses = explode(',',$houses);
		$allow = $this->_checkAllows($houses, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		if ($houseService->setState($houses,0)) {
			echo '房源上架成功'."\treload";
			ajax_footer();
		}
	}
	
	/**
	 * 刷新一个房源
	 * @param integer $houseid 房子id
	 * @param string  $type	        房子类型，sale或hire
	 */
	function refresh($houseid, $type) {
		$allow = $this->_checkAllows($houseid, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		if ($houseService->refresh($houseid)) {
			echo getLangInfo('msg','operate_success')."\treload";
			ajax_footer();
		}
	}
	
	/**
	 * 加急房源交互
	 * @param integer $houseid 房子id
	 * @param string  $type	        房子类型，sale或hire
	 */
	function urgent($houseid, $type) {
		global $winduid;
		if (!$houseid) ERROR::showError('数据有误');
		$allow = $this->_checkAllows($houseid, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		$expiredService = House::loadService('expiredService');
		require_once(R_P.'require/credit.php');
		if ($type == 'hire') {
			$info = $houseService->getHireInfo($houseid);
			$house_rapidprice = $GLOBALS['house_hire_rapidprice'];
			$house_rapidcredit = $credit->cType[$GLOBALS['house_hire_rapidcredit']];
			$myCreditNum = $credit->get($winduid,$GLOBALS['house_hire_rapidcredit']);
			$expiredTime = $expiredService->getsExpire($houseid, 'hire', 'urgent');
		} else {
			$info = $houseService->getSaleInfo($houseid);
			$house_rapidprice = $GLOBALS['house_sale_rapidprice'];
			$house_rapidcredit = $credit->cType[$GLOBALS['house_sale_rapidcredit']];
			$myCreditNum = $credit->get($winduid,$GLOBALS['house_sale_rapidcredit']);
			$expiredTime = $expiredService->getsExpire($houseid, 'sale', 'urgent');
		}
		return array($info, $house_rapidprice, $house_rapidcredit, $myCreditNum, $expiredTime);
	}
	
	/**
	 * 加急一个房源
	 * @param integer $houseid 房子id
	 * @param integer $days    天数
	 * @param string  $type	        房子类型，sale或hire
	 */
	function dourgent($houseid, $days, $type, $isurgent, $isback, $ismsg) {
		global $winduid;
		$houseid = (int) $houseid;
		$isurgent = (int) $isurgent;
		$isback = (int) $isback;
		$ismsg = (int) $ismsg;
		if (!$houseid || $days<1 || !is_numeric($isurgent) || !is_numeric($isback) || !is_numeric($ismsg)) ERROR::showError('数据有误');
		$allow = $this->_checkAllows($houseid, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		!$type && $type = 'sale';
		if (isHouseManager($winduid, $type)) {
			if ($houseService->setUrgent($houseid, $isurgent, $days, $isback, $ismsg)) {
				echo getLangInfo('msg','operate_success')."\treload";
				ajax_footer();
			}
		} else {
			require_once(R_P.'require/credit.php');
			$house_rapidprice = $GLOBALS['house_'. $type .'_rapidprice'];
			$house_rapidcredit = $GLOBALS['house_'. $type .'_rapidcredit'];
			$totalCreditNum = $days * $house_rapidprice;
			$myCreditNum = $credit->get($winduid,$house_rapidcredit);
			if ($totalCreditNum > $myCreditNum) ERROR::showError('您的积分不足，不能完成该操作');
			$credit->set($winduid,$house_rapidcredit,-$totalCreditNum);
			if ($houseService->setUrgent($houseid,1, $days)) {
				echo getLangInfo('msg','operate_success')."\treload";
				ajax_footer();
			}
		}
	}
	
	/**
	 * 置顶房源交互
	 * @param integer $houseid 房子id
	 * @param string  $type	        房子类型，sale或hire
	 */
	function top($houseid, $type) {
		global $winduid;
		if (!$houseid) ERROR::showError('数据有误');
		$allow = $this->_checkAllows($houseid, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		$expiredService = House::loadService('expiredService');
		require_once(R_P.'require/credit.php');
		if ($type == 'hire') {
			$info = $houseService->getHireInfo($houseid);
			$house_topprice = $GLOBALS['house_hire_topprice'];
			$house_topcredit = $credit->cType[$GLOBALS['house_hire_topcredit']];
			$myCreditNum = $credit->get($winduid,$GLOBALS['house_hire_topcredit']);
			$expiredTime = $expiredService->getsExpire($houseid, 'hire', 'top');
		} else {
			$info = $houseService->getSaleInfo($houseid);
			$house_topprice = $GLOBALS['house_sale_topprice'];
			$house_topcredit = $credit->cType[$GLOBALS['house_sale_topcredit']];
			$myCreditNum = $credit->get($winduid,$GLOBALS['house_sale_topcredit']);
			$expiredTime = $expiredService->getsExpire($houseid, 'sale', 'top');
		}
		return array($info, $house_topprice, $house_topcredit, $myCreditNum, $expiredTime);
	}
	
	/**
	 * 置顶一个房源
	 * @param integer $houseid 房子id
	 * @param integer $days    天数
	 * @param string  $type	        房子类型，sale或hire
	 */
	function dotop($houseid, $days, $type, $istop, $isback, $ismsg) {
		global $winduid;
		$houseid = (int) $houseid;
		$istop = (int) $istop;
		$isback = (int) $isback;
		$ismsg = (int) $ismsg;
		if (!$houseid || $days<1 || !is_numeric($istop) || !is_numeric($isback) || !is_numeric($ismsg)) ERROR::showError('数据有误');
		$allow = $this->_checkAllows($houseid, $type);
		if (!$allow) {
			echo '非法操作';
			exit;
		}
		$houseService = $this->_getService($type);
		!$type && $type = 'sale';
		if (isHouseManager($winduid, $type)) {
			if ($houseService->setTop($houseid, $istop, $days, $isback, $ismsg)) {
				echo getLangInfo('msg','operate_success')."\treload";
				ajax_footer();
			}
		} else {
			require_once(R_P.'require/credit.php');
			$totalCreditNum = $days * $GLOBALS['house_'.$type.'_topprice'];
			$myCreditNum = $credit->get($winduid,$GLOBALS['house_'.$type.'_topcredit']);
			if ($totalCreditNum > $myCreditNum) ERROR::showError('您的积分不足，不能完成该操作');
			$credit->set($winduid,$GLOBALS['house_'.$type.'_topcredit'],-$totalCreditNum);
			if ($houseService->setTop($houseid,1, $days)) {
				echo getLangInfo('msg','operate_success')."\treload";
				ajax_footer();
			}
		}
	}
	
	/**
	 * 检查是否有权限进行相应的一个或多个操作
	 * @param mixed $houseids  房子id
	 * @param string  $type	        房子类型，sale或hire
	 */
	function _checkAllows($houseids, $type) {
		global $winduid, $groupid;
		is_numeric($houseids) && $houseids = array($houseids);
		if (!S::isArray($houseids)) return false;
		$houseService = $this->_getService($type);
		$houseInfos = $houseService->getUidsByHouseid($houseids);
		foreach ($houseInfos as $key => $value) {
			if ($winduid != $value['uid'] && $groupid != 3) return false;
		}
		return true;
	}
	
	/**
	 * 根据类型获取相应的服务
	 * @param string $type 房子类型，sale或hire
	 */
	function _getService($type) {
		if ($type == 'hire') return $this->getHouseHireService();
		return $this->getHouseSaleService();
	}
}
?>